﻿using UnityEngine;
using System.Collections;

//[ExecuteInEditMode]
public class MeshCtor : MonoBehaviour {



    void OnGUI()
    {
		if (GUI.Button(new Rect(10, 10, 150, 100), "test")) CreateTrangle();
    }

    public Mesh mMesh;			  // First generated mesh
    public MeshFilter mFilter;    // Mesh filter for this draw call

    void CreateTrangle() 
    {
        mMesh = new Mesh();
        //mMesh.hideFlags = HideFlags.DontSave;
        mMesh.MarkDynamic();
        //1   2 
        //0   3
        Vector3[] verts = new Vector3[3];
        Vector2[] uvs = new Vector2[verts.Length];
        int vert_index = 0;
        int uv_index = 0;
        for (int i = 0; i < verts.Length; i += 4)
        {
            verts[vert_index++] = new Vector3(0, 0, 0); //top-left
            verts[vert_index++] = new Vector3(0, 1, 0); //bottom-left
            verts[vert_index++] = new Vector3(1, 1, 0); //top-right

            uvs[uv_index++] = new Vector2(0, 0);
            uvs[uv_index++] = new Vector2(0, 1);
            uvs[uv_index++] = new Vector2(1, 1);
        }

            
        int[] triangles = new int[(verts.Length >> 1) * 3];
        int triangle_index = 0;
        for (int i = 0; i < verts.Length; i += 4)
        {
            triangles[triangle_index++] = i;
            triangles[triangle_index++] = i + 1;
            triangles[triangle_index++] = i + 2;
        }

        mMesh.vertices = verts;
        mMesh.uv = uvs;
        mMesh.triangles = triangles;
        ////Shader wants normals, but the mesh  doesn't have them
        mMesh.RecalculateNormals();

        mFilter.mesh = mMesh;
    }

    /// <summary>
    /// Local space region where the actual drawing will take place.
    /// X = left, Y = bottom, Z = right, W = top.
    /// </summary>
    //Vector4 v = drawingDimensions;
    //Vector4 u = drawingUVs;
    void UpdateGeometryV2()
    {

        

        mMesh = new Mesh();
        //mMesh.hideFlags = HideFlags.DontSave;
        mMesh.MarkDynamic();

        //1   2 
        //0   3
        BetterList<Vector3> verts = new BetterList<Vector3>();
        BetterList<Vector2> uvs = new BetterList<Vector2>();
        BetterList<int> triangles = new BetterList<int>();
        int panel_count = 5;
        for (int i = 0; i < panel_count; i++)
        {
            verts.Add(Vector3.zero);
            verts.Add(Quaternion.AngleAxis(-72.0f * i, Vector3.forward) * Vector3.up);
            verts.Add(Quaternion.AngleAxis(-72.0f * (i+1), Vector3.forward) * Vector3.up);
            //verts.Add(Vector3.up * Quaternion.);
            //verts.Add(new Vector3(0, 0, 0)); //top-left
            //verts.Add(new Vector3(0, 1, 0)); //bottom-left
            //verts.Add(new Vector3(1, 1, 0)); //top-right

            uvs.Add(new Vector2(0, 0));
            uvs.Add(new Vector2(0, 1));
            uvs.Add(new Vector2(1, 1));

            triangles.Add(3 * i);
            triangles.Add(3 * i + 1);
            triangles.Add(3 * i + 2);
        }

        mMesh.vertices = verts.ToArray();
        mMesh.uv = uvs.ToArray();
        mMesh.triangles = triangles.ToArray();
        //GenerateCachedIndexBuffer(verts.size, (verts.size >> 1) * 3);
        mMesh.RecalculateNormals();

        mFilter.mesh = mMesh;
    }

    int[] GenerateCachedIndexBuffer(int vertexCount, int indexCount)
    {
        int[] rv = new int[indexCount];
        int index = 0;

        for (int i = 0; i < vertexCount; i += 4)
        {
            rv[index++] = i;
            rv[index++] = i + 1;
            rv[index++] = i + 2;
        }
        return rv;
    }

    ///五维图
    ///Tp 导图时候的顶点变换
}
